/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tuxiaobei.drawandguess.component;

import com.tuxiaobei.drawandguess.ResourceTable;
import com.tuxiaobei.drawandguess.bean.DeviceData;
import com.tuxiaobei.drawandguess.component.listcomponent.CommentViewHolder;
import com.tuxiaobei.drawandguess.component.listcomponent.ListComponentAdapter;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import java.util.Arrays;


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

/**
 * WordSelectDialog
 *
 * @since 2022-05-04
 */
public class ChangeName extends CommonDialog {
    private static final int CORNER_RADIUS = 10;
    private final Context context;
    private OnclickListener onclickListener;
    private TextField name;
    private String now_name;
    private Text operateConfirm;
    private Text operateCancel;

    /**
     * Interfaces for setting the OK button and canceling clicks
     *
     * @since 2021-04-06
     */
    public interface OnclickListener {
        /**
         * Used for interface callback.
         *
         * @param word select word
         */
        void onConfirmClick(String word);
    }

    /**
     * setListener
     *
     * @param context context
     */
    public ChangeName(Context context, String now_name) {
        super(context);
        this.context = context;
        this.now_name = now_name;
    }

    /**
     * setListener
     *
     * @param listener listener
     */
    public void setListener(OnclickListener listener) {
        onclickListener = listener;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
    }

    private void initView() {
        Component rootView = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_dialog_layout_name, null, false);
        if (rootView.findComponentById(ResourceTable.Id_name) instanceof TextField) {
            name = (TextField) rootView.findComponentById(ResourceTable.Id_name);
            name.setTextCursorVisible(false);
        }
        if (rootView.findComponentById(ResourceTable.Id_operate_yes) instanceof Text) {
            operateConfirm = (Text) rootView.findComponentById(ResourceTable.Id_operate_yes);
        }
        if (rootView.findComponentById(ResourceTable.Id_operate_no) instanceof Text) {
            operateCancel = (Text) rootView.findComponentById(ResourceTable.Id_operate_no);
        }
        setSize(MATCH_PARENT, MATCH_CONTENT);
        setAlignment(LayoutAlignment.BOTTOM);
        setCornerRadius(CORNER_RADIUS);
        setAutoClosable(true);
        setContentCustomComponent(rootView);
        setTransparent(true);
        name.setText(now_name);
        componentBonding();
    }

    private void componentBonding() {
        operateConfirm.setClickedListener(component -> {
            if (onclickListener != null) {
                setName();
            }
        });
        operateCancel.setClickedListener(component -> hide());
    }

    private void setName() {
        onclickListener.onConfirmClick(name.getText());
    }
}
